package com.kaifamiao.month03.day0330;

public class QuickSort {

    public static void quicksort(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int temp = arr[left];
        int l = left;
        int r = right;
        while (l < r) {
            while (arr[r] >= temp && l < r) {
                r--;
            }
            while (arr[l] <= temp && l < r) {
                l++;
            }
            int t = arr[l];
            arr[l] = arr[r];
            arr[r] = t;
        }
        arr[left] = arr[r];
        arr[r] = temp;
        quicksort(arr, left, r - 1);
        quicksort(arr, r + 1, right);
    }

    public static int search(int[] arr, int key, int start, int end) {
        if (start > end) {
            return -1;
        }
        int mid = (start + end) >>> 1;
        if (arr[mid] == key ) {
            return mid;
        }else if (arr[mid] > key) {
            end = mid - 1;
        }else {
            start = mid + 1;
        }
        return search(arr, key, start, end);
    }

    public static void main(String[] args) {
        int[] arr = {2, 1, 0, 2, 8, 4, 6};
        quicksort(arr, 0, arr.length - 1);
        for (int a : arr) {
            System.out.print(a);
        }
        System.out.println();
        System.out.println(search(arr, 2, 0, arr.length - 1));
    }
}
